<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/24
 * Time: 11:57
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\UserTrue;
use app\api\service\LqbService;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\LqbAddPhone;
use app\common\controller\UserPhone;

class Lqb extends BaseController
{
    public $service;
    public function __construct()
    {
        $this->service = new lqbService();
    }

    /**
     * 获取单个商品的api
     * @param $id
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     * 400 =>商品已下架  401 => 商品不存在
     */
    public function getOneProduct($id)
    {
        (new IDMustBePositiveInt()) -> goCheck();
        $result = $this->service->getOneProduct($id);
        return json([
            'code' => 200,
            'data' => $result['data'],
            'in' => $result['in']
        ]);
    }

    /**
     * 首页展示
     * @return \think\response\Json
     */
    public function showAll()
    {
        $result = $this->service->showAll();
        return json([
           'code' => 200,
            'data' => $result
        ]);
    }

    /**
     * 根据分类id进行分类
     * @param $id
     * @return \think\response\Json
     * @throws \app\lib\exception\MysqlErrorException
     * @throws \app\lib\exception\ParameterException
     */
    public function category($id)
    {
        (new IDMustBePositiveInt()) -> goCheck();
        $result = $this->service->getCategoryProduct($id);
        return json([
            'code' => 200,
            'data' => $result
        ]);
    }

    /**
     * 分页接口
     * @param $get_data 获取get参数
     * @return \think\response\Json
     */
    public function getProductPage($page, $limit = 5)
    {
        $result = $this->service->getProductPage($page, $limit);
        return json($result);
    }

    /**
     * 记录前端申请次数按钮
     * @param $id
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function getRecord($id)
    {
        $result = $this->service->writeRecordToRedis($id);
        return json($result);
    }

    /**
     * 添加app注册的手机号码
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function addPhone()
    {
        $post = (new LqbAddPhone())->goCheck();
        $service = new UserPhone();
        $service->add_user_true($post['phone'], $post['logo'], 3);
        return json([
           'code' => 200,
           'success' => true
        ]);
    }
}